package com.buss.core.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Transient;

import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name="core_user")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@TableGenerator(name="user_pk_sequence",initialValue=1,allocationSize=1,pkColumnName="seq_col",
pkColumnValue="user_seq",valueColumnName="seq_val",table="core_pk_sequence")
public class User implements Serializable{

	private static final long serialVersionUID = 3061170495371608450L;
	
	public User(){}
	
	public User(Long id,String userId){
		super();
		this.id = id;
		this.userId = userId;
	}
	
	@Id
	@GeneratedValue(strategy=GenerationType.TABLE,generator="user_pk_sequence")
	private Long id;
	
	
	/**登陆用户编码*/	
	@Column(length = 20,nullable=false)
	@JsonProperty(value="userId")
	private String userId;
	
	/**
	 * 单位编码
	 */
	@Column(length = 50,nullable=false)
	private String companyId;
	
	/**用户名*/
	@Column(nullable=false,length = 20)
	private String userName;
	
	/**用户密码*/
	@Column(length = 100,updatable=false)
	@JsonDeserialize
	private String userPass;
	
	/**电子邮件*/
	@Column(length = 100)
	private String email;
	
	/**联系电话*/
	@Column(length = 20)
	private String telephone;
	
	/**移动电话*/
	@Column(length = 20)
	private String mobilePhone;
	
	/**是否停用*/
	private boolean disabled;
	
	/**是否系统管理员*/
	private boolean sysAdmin;
	
	/**是否业务员*/
	private Boolean isSalesMan;
	
	/**是否选中*/
	@Transient
	private Boolean checked;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	/**用户角色信息*/
	@JsonDeserialize
	@ManyToMany(cascade=CascadeType.REFRESH,fetch=FetchType.LAZY)
	@JoinTable(name="core_role_user",inverseJoinColumns=@JoinColumn(name="roleId",updatable=false),
			joinColumns=@JoinColumn(name="userId",updatable=false))	
	private List<Role> roles = new ArrayList<Role>();

	/**
	 * 用户所属组织机构
	 */
	@JsonDeserialize
	@ManyToOne(cascade = CascadeType.REFRESH)
	@JoinColumn(name="organId",nullable=false)
	private Organization organ;
	
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getCompanyId() {
		return companyId;
	}

	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPass() {
		return userPass;
	}

	public void setUserPass(String userPass) {
		this.userPass = userPass;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getMobilePhone() {
		return mobilePhone;
	}

	public void setMobilePhone(String mobilePhone) {
		this.mobilePhone = mobilePhone;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public Boolean getIsSalesMan() {
		return isSalesMan;
	}

	public void setIsSalesMan(Boolean isSalesMan) {
		this.isSalesMan = isSalesMan;
	}

	public boolean isSysAdmin() {
		return sysAdmin;
	}

	public void setSysAdmin(boolean sysAdmin) {
		this.sysAdmin = sysAdmin;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public Organization getOrgan() {
		return organ;
	}

	public void setOrgan(Organization organ) {
		this.organ = organ;
	}

	public Boolean getChecked() {
		return checked;
	}

	public void setChecked(Boolean checked) {
		this.checked = checked;
	}

}
